import os
from utils.read_data import FileParse
from utils.api_handler import MyRequest

class BaseController:

    __TOKEN = None

    def __init__(self):

        root_dir = os.path.dirname(os.path.dirname(__file__))
        config_yaml = os.path.join(root_dir,'config/config.yaml')
        config_data = FileParse.parse_yaml_data(config_yaml)
        print(config_data)
        self.url = config_data.get('base_url')
        self.base_url = self.url+"/adminapi/login"
        self.user = config_data.get("account")
        self.key = config_data.get("key")
        self.pwd = config_data.get("pwd")
        self.headers = config_data.get("headers")
        self.requests = MyRequest()

        if BaseController.__TOKEN == None:
            BaseController.__TOKEN = self.__get_token()
        self.token = BaseController.__TOKEN


    def __get_token(self):
        data = {"account":self.user,"pwd":self.pwd,"key":self.key}
        print(data)
        res = self.requests.post(url=self.base_url,json=data,headers=self.headers)
        print(res.json())
        print('获取token的值为:',res.json()['data']['token'])
        return res.json()['data']['token']





if __name__ == '__main__':
    BaseController()
